package com.peekay.mwseer;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import com.peekay.mwseer.databinding.ActivityMainBinding;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        binding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());
        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        sharedPreferences = getSharedPreferences("settings", 0);
        binding.drawer.changeZz.setChecked(sharedPreferences.getBoolean("change_zz", true));
        binding.drawer.closeMusic.setChecked(sharedPreferences.getBoolean("close_music", true));
        binding.game.getSettings().setJavaScriptEnabled(true);
        binding.game.getSettings().setMediaPlaybackRequiresUserGesture(false);
//        binding.game.getSettings().setSaveFormData(true);//设置可以保存表单数据
        //binding.game.getSettings().setSavePassword(true);//设置可以保存密码
        binding.game.getSettings().setAppCacheEnabled(true);//设置APP可以缓存
        binding.game.getSettings().setDomStorageEnabled(true);//返回上个界面不刷新 允许本地缓存
        binding.game.getSettings().setAllowFileAccess(true);//允许访问文件
        binding.game.loadUrl("http://seerh5.61.com");
        binding.drawer.refresh.setOnClickListener(view -> {
            //刷新游戏
            binding.game.reload();
            binding.drawerLayout.closeDrawer(Gravity.LEFT);
        });
        binding.drawer.reCreate.setOnClickListener(view -> {
            //重启软件
            finish();
            startActivity(new Intent(this, MainActivity.class));
        });
        binding.setting.setOnClickListener(view -> binding.drawerLayout.openDrawer(Gravity.LEFT));
        binding.setting.setOnLongClickListener(view -> {
            binding.setting.setVisibility(View.GONE);
            return true;
        });
        binding.drawer.changeZz.setOnCheckedChangeListener((compoundButton, b) -> {
            sharedPreferences.edit().putBoolean("change_zz", b).apply();
            Toast.makeText(this, "加载过的精灵可能需刷新生效", Toast.LENGTH_SHORT).show();
        });
        binding.drawer.closeMusic.setOnCheckedChangeListener((compoundButton, b) -> {
            sharedPreferences.edit().putBoolean("close_music", b).apply();
            Toast.makeText(this, "加载过的音乐可能需刷新生效", Toast.LENGTH_SHORT).show();
        });
        binding.drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                // 主页内容
                View content = binding.drawerLayout.getChildAt(0);
                //侧边栏
                View menu = drawerView;
                content.setTranslationX(menu.getMeasuredWidth() * slideOffset);//0~width
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
        binding.game.setWebViewClient(new WebViewClient() {
            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                try {
                    if (binding.drawer.closeMusic.isChecked()) {
                        //游戏静音
                        if (request.getUrl().toString().endsWith(".mp3")) {
                            InputStream is = null;
                            return new WebResourceResponse("audio/x-mpeg-3", "UTF-8", is);
                        }
                    }
                    //精灵变粽子
                    if (binding.drawer.changeZz.isChecked()) {
                        if (request.getUrl().toString().contains("resource/cjs_animate/preview/") && request.getUrl().toString().contains(".png")) {
                            InputStream is = getAssets().open("preview_1702_atlas_.png");
                            return new WebResourceResponse("image/png", "UTF-8", is);
                        }
                        if (request.getUrl().toString().contains("resource/cjs_animate/pet/") && request.getUrl().toString().contains(".png")) {
                            InputStream is = getAssets().open("pet_1702_atlas_.png");
                            return new WebResourceResponse("image/png", "UTF-8", is);
                        }
                        if (request.getUrl().toString().contains("resource/cjs_animate/preview/") && request.getUrl().toString().endsWith(".js")) {
                            InputStream is = getAssets().open("preview_1702.js");
                            return new WebResourceResponse("application/x-javascript", "UTF-8", is);
                        }
                        if (request.getUrl().toString().contains("resource/cjs_animate/pet/") && request.getUrl().toString().endsWith(".js")) {
                            InputStream is = getAssets().open("pet_1702.js");
                            return new WebResourceResponse("application/x-javascript", "UTF-8", is);
                        }
                        if (request.getUrl().toString().contains("resource/assets/fightResource/") && request.getUrl().toString().contains(".png")) {
                            InputStream is = getAssets().open("fight_1702.png");
                            return new WebResourceResponse("image/png", "UTF-8", is);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return super.shouldInterceptRequest(view, request);
            }
        });
    }
}